<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
</head>
<body>

<p>全局变量计数。</p>
<button type="button" onclick="myFunction()">计数!</button>
<p id="demo">0</p>
<script>

    /*全局变量不会销毁 因为函数中保持了对它的引用*/
    var counter = 0;
    function add() {
        return counter += 1;
    }
    function myFunction(){
        document.getElementById("demo").innerHTML = add();
        console.log(counter)
    }




</script>

</body>
</html>